package br.com.localfloripa.controllers;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ResourceBundle;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.localfloripa.entities.TipoImagem;
import br.com.localfloripa.util.ImageUtils;


public class ImageServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	private static final Logger log = LoggerFactory.getLogger(ImageServlet.class);
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ImageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// Get the absolute path of the image
	    ServletContext context = getServletContext();
	    ResourceBundle resource = ResourceBundle.getBundle("messages.MessageResources");
	    String filename = resource.getString("path.files.images.base")+request.getParameter("photo");
	    if (filename != null && !filename.contains("noimage")) {
	    	String type = request.getParameter("type"); 

		    // Get the MIME type of the image
		    String mimeType = context.getMimeType(filename);
		    if (mimeType == null) {
		        context.log("Could not get MIME type of "+filename);
		        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		        return;
		    }

		    // Set content type
		    response.setContentType(mimeType);

		    // Open the file and output streams
		    FileInputStream fis = null;
			try {
				// Set content size
			    File file = new File(filename);
				fis = new FileInputStream(file);
				BufferedImage image = null;
			    /*byte is = 0b;*/
			    if (TipoImagem.ORIGINAL.value().equals(type)) {
			    	image = ImageUtils.resizeImage(fis, 400, 500);
			    } else if(TipoImagem.THUMB.value().equals(type)) {
			    	image = ImageUtils.resizeImage(fis, 150, 150);
			    } else if(TipoImagem.LOGO.value().equals(type)) {
			    	image = ImageUtils.resizeImage(fis, 200, 200);
			    } else if(TipoImagem.TEMPLATE.value().equals(type)) {
			    	image = ImageUtils.resizeImage(fis, 137, 248);
			    } else if(TipoImagem.PREVIEW.value().equals(type)) {
			    	image = ImageUtils.resizeImage(fis, 120, 120);
			    } else {
			    	image = ImageUtils.resizeImage(fis, 180, 200);
			    } 
			    ImageIO.write(image, "jpg", response.getOutputStream());
			} catch (FileNotFoundException e) {
				BufferedImage image = null;
				
				fis = new FileInputStream(resource.getString("path.files.images.base.noimage"));
				if (TipoImagem.ORIGINAL.value().equals(type)) {
			    	image = ImageUtils.resizeImage(fis, 400, 400);
			    } else if(TipoImagem.THUMB.value().equals(type)) {
			    	image = ImageUtils.resizeImage(fis, 150, 150);
			    } else if(TipoImagem.LOGO.value().equals(type)) {
			    	image = ImageUtils.resizeImage(fis, 200, 200);
			    } else if(TipoImagem.TEMPLATE.value().equals(type)) {
			    	image = ImageUtils.resizeImage(fis, 137, 350);
			    } else if(TipoImagem.PREVIEW.value().equals(type)) {
			    	image = ImageUtils.resizeImage(fis, 120, 120);
			    } else {
			    	image = ImageUtils.resizeImage(fis, 200, 145);
			    } 
			    ImageIO.write(image, "gif", response.getOutputStream());		
			
				log.warn(e.getMessage(),e);
			} finally {
				if (fis != null)
					fis.close();
			}
	    } else {
	    	FileInputStream fis = null;
	    	fis = new FileInputStream(resource.getString("path.files.images.base.noimage"));
	    	BufferedImage image = ImageUtils.resizeImage(fis, 200, 145);;
	    	ImageIO.write(image, "gif", response.getOutputStream());
	    	fis.close();
	    }
	    			
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
